<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var Matrix = function (upper, lower, colsum) {
      var zeroArr = colsum.slice().fill(0)
      var [row1, row2] = [[...zeroArr], [...zeroArr]]
      for (var i = 0; i < colsum.length; i++) {
        if (colsum[i] == 2) {
          row1[i] = row2[i] = 1
          upper--
          lower--
        }
      }
      if (upper < 0 || lower < 0) return []
      for (var i = 0; i < colsum.length; i++) {
        if (colsum[i] == 1) {
          if (upper > 0) {
            row1[i] = 1
            row2[i] = 0
            upper--
          }
          else if (lower > 0) {
            row1[i] = 0
            row2[i] = 1
            lower--
          }
          else {
            return []
          }
        }
      }
      if (upper > 0 || lower > 0) return []
      return [row1, row2]
    };
    console.log(Matrix(upper = 2, lower = 1, colsum = [1, 1, 1]))
  </script>
</body>

</html>